<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            height: 400px;
            width: 100%;
            border-left: 3px solid red;
            border-top: 3px solid red;
        }
        .nav {
            height: 100%;
            width: 100px;
            float: left;
            box-sizing: border-box;
        }
        .nav-item {
            height: 25%;
            width: 100%;
            text-align: center;
            /* 文字纵向居中 start */
            display: flex;
            flex-direction: column;
            justify-content: center;
            /* 文字纵向居中 end */
            box-sizing: border-box;
            border-right: 3px solid red;
            border-bottom: 3px solid red;
        }
        /* .nav-item:hover {
            background-color: red;
        } */
        .nav-item.active {
            background-color: red;
        }
        .intro {
            height: 100%;
            /* calculate 计算 */
            width: calc(100% - 100px);
            border-right: 3px solid red;
            border-bottom: 3px solid red;
            float: left;
            box-sizing: border-box;
        }
    </style>
</head>
<body>

    <div class="container">
        <div class="nav">
            <!-- to do -->
        </div>
        <div class="intro"></div>
    </div>

    <script src="../../../demo/jq/jq.js"></script>

    <script>

        const data = [
            {
                color: 'red',
                title: '女靴',
            },
            {
                color: 'aqua',
                title: '雪地靴',
            },
            {
                color: 'pink',
                title: '冬裙',
            },
            {
                color: 'blue',
                title: '呢大衣',
            },
            {
                color: 'black',
                title: 'xxxx',
            },
        ];

        // 写一个初始化函数
        // 把数组的每一项渲染到新建的dom元素中
        function createListItems () {
            for (const item of data) {
                // 创建元素
                const el = $(`
                    <div class="nav-item" style="height: ${ 100 / data.length }%">
                        ${ item.title }
                    </div>
                `);
                $('.nav').append(el);
            }
            $('.nav-item').on('mouseover', function (e) {
                e.stopPropagation();
                const index = $(this).index();
                currentIndex = index;
                $('.intro').css('background', data[index].color);
                $(this).addClass('active').siblings().removeClass('active');
            });
        }

        $('.intro').css('background', data[0].color);

        createListItems();

        $('.nav-item').eq(0).addClass('active');

        let currentIndex = 1;
        let timer = null;

        function run () {
            timer = setTimeout(function () {
                $('.nav-item').eq(currentIndex % data.length).trigger('mouseover');
                currentIndex++;
                // 在setTimeout结束后继续启用setTimeout  达到interval的效果
                run();
            }, 1000);
        }

        run();

        $('.container').on('mouseover', function (e) {
            clearTimeout(timer);
        });

        $('.container').on('mouseleave', run);

    </script>
</body>
</html>